/**
 * @filename:OtherPieceServiceImpl Apr 19, 2024
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.service.impl;

import com.wonder.app.domain.IpadUser;
import com.wonder.app.domain.Material;
import com.wonder.app.domain.OtherPiece;
import com.wonder.app.mapper.IpadUserDao;
import com.wonder.app.mapper.MaterialDao;
import com.wonder.app.mapper.OtherPieceDao;
import com.wonder.app.service.OtherPieceService;
import com.wonder.app.request.OtherPieceRequest;
import com.wonder.common.exception.CustomException;
import com.wonder.common.utils.DateUtils;
import com.wonder.common.utils.StringUtils;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.common.AjaxResult;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.Date;

/**   
 * @Description:TODO(其他计件服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 * 
 */
@Service
public class OtherPieceServiceImpl  extends ServiceImpl<OtherPieceDao, OtherPiece> implements OtherPieceService  {


	@Autowired
	private IpadUserDao ipadUserDao;

	@Autowired
	private MaterialDao materialDao;

	@Autowired
	private OtherPieceDao otherPieceDao;

	@Autowired
	private OtherPieceService otherPieceService;
    /**
     * 验证其他计件信息
     * 
     * @param otherPiece
     * @param type
     * @return String
     */
    @Override
    public String validate(OtherPiece otherPiece,String type){
    
    
       return "";
    }
    
    
    /**
	 * Description:其他计件分页返回
	 * @param request //检索条件
	 * @param page //分页参数
	 * @return IPage<OtherPiece>   //分页记录
	 */
	@Override 
	public IPage<OtherPiece> selectList(OtherPieceRequest request, Page<OtherPiece> page){
	    LambdaQueryWrapper<OtherPiece> lambdaQueryWrapper = new LambdaQueryWrapper<>();
	    
	    if (null != request.getId()) {
	       lambdaQueryWrapper.eq(OtherPiece::getId, request.getId());
	      }               
             
        if (StringUtils.hasText(request.getUserName())) {
			    lambdaQueryWrapper.like(OtherPiece::getUserName, request.getUserName()); 
		    } 
             
        if (request.getCreateTime() != null){
        	Date[] createTime=request.getCreateTime();
        	if(createTime.length>0)
        	  lambdaQueryWrapper.ge(OtherPiece::getCreateTime, createTime[0]); 
        	if(createTime.length==2)
        	  lambdaQueryWrapper.le(OtherPiece::getCreateTime, DateUtils.dateToStr(createTime[1],"yyyy-MM-dd")+ " 23:59:59");
        	
        }    
             
        
		lambdaQueryWrapper.orderByDesc(OtherPiece::getId);	
		Page<OtherPiece> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
		return selectPage;
    }

	@Override
	public boolean saveOtherPiece(OtherPiece entity) throws CustomException {
		String userName = entity.getUserName();
		IpadUser ipadUser = ipadUserDao.selectOne(new LambdaQueryWrapper<IpadUser>().eq(IpadUser::getFname, userName).last("limit 1"));
		if (null == ipadUser){
			throw new CustomException("用户名不存在");
		}
		Long userId = ipadUser.getId();
		entity.setUserId(userId);
		String materialNo = entity.getMaterialNo();
		Material material = materialDao.selectOne(new LambdaQueryWrapper<Material>().eq(Material::getFNUMBER, materialNo));
		String materialName = material.getFName();
		entity.setMaterialName(materialName);
		Double piece = entity.getPiece();//计件单价
		Double count = entity.getCount();//计件数量
		entity.setTotalCount(piece * count);
		boolean save = otherPieceService.save(entity);
		return save;
	}

	@Override
	public boolean updateOtherPiece(OtherPiece entity) {
		String userName = entity.getUserName();
		IpadUser ipadUser = ipadUserDao.selectOne(new LambdaQueryWrapper<IpadUser>().eq(IpadUser::getFname, userName).last("limit 1"));
		if (null == ipadUser){
			throw new CustomException("用户名不存在");
		}
		Long userId = ipadUser.getId();
		entity.setUserId(userId);
		String materialNo = entity.getMaterialNo();
		Material material = materialDao.selectOne(new LambdaQueryWrapper<Material>().eq(Material::getFNUMBER, materialNo));
		String materialName = material.getFName();
		entity.setMaterialName(materialName);
		Double piece = entity.getPiece();//计件单价
		Double count = entity.getCount();//计件数量
		entity.setTotalCount(piece * count);
		boolean save = otherPieceService.saveOrUpdate(entity);
		return save;
	}


}